﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.IO.Compression;

namespace LeadVinaglue
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        //public MvcApplication()
        //{
        //    InitializeComponent();
        //}

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            //With top product
            routes.MapRoute(
               "TopProduct",
               "liet-ke-tai-lieu.0.0.html/{page}.{vt}",
               new
               {
                   controller = "Product",
                   action = "TopProduct",
                   page = 1,
                   vt = "moi-nhat"
               }
            );
            // With Product detail
            routes.MapRoute(
               "Detail",
               "xem-tai-lieu/{title}.{productId}.html",
               new
               {
                   controller = "Product",
                   action = "Detail",
                   title = string.Empty,
                   productId = 0
               }
            );
            
            // With Search
            routes.MapRoute(
               "SearchPage",
               "tags/tai-lieu/{keywords}",
               new
               {
                   controller = "Product",
                   action = "Search",
                   keywords = string.Empty
               }
            );

            // With sub
            routes.MapRoute(
               "SubCategoryList",
               "{categoryName}/{subCategoryName}.{categoryId}.{subCategoryId}",
               new
               {
                   controller = "Product",
                   action = "CategoryList",
                   categoryId = 0,
                   subCategoryId = 0
               },
               new { categoryId = @"\d{1,8}", subCategoryId = @"\d{1,8}" }
            );

            // With category
            routes.MapRoute(
               "CategoryList",
               "{categoryName}/{subCategoryName}.{categoryId}",
               new
               {
                   controller = "Product",
                   action = "CategoryList",
                   categoryId = 0,
                   subCategoryId = 0
               },
               new { categoryId = @"\d{1,8}", subCategoryId = @"\d{1,8}" }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );


            // Error
            routes.MapRoute(
                "404Error",
                "404",
                new { controller = "Error", action = "Index" }
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
        
        private void InitializeComponent()
        {
            this.PostReleaseRequestState += new EventHandler(Global_PostReleaseRequestState);
        }

        private void Global_PostReleaseRequestState(object sender, EventArgs e)
        {
            string contentType = Response.ContentType;

            if (contentType == "text/html" || contentType == "text/css")
            {
                Response.Cache.VaryByHeaders["Accept-Encoding"] = true;

                string acceptEncoding =
                    Request.Headers["Accept-Encoding"];

                if (acceptEncoding != null)
                {
                    if (acceptEncoding.Contains("gzip"))
                    {
                        Response.Filter = new GZipStream(
                            Response.Filter, CompressionMode.Compress);
                        Response.AppendHeader(
                            "Content-Encoding", "gzip");
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        Response.Filter = new DeflateStream(
                            Response.Filter, CompressionMode.Compress);
                        Response.AppendHeader(
                            "Content-Encoding", "deflate");
                    }
                }
            }
        } 
    }
}